import {
    selectAll,
    add,
    update,
    del,
    allSave,
    determine
} from '@/api/order'

const getDefaultState = () => {
    return {}
}

const state = getDefaultState()

const mutations = {
    RESET_STATE: (state) => {
        Object.assign(state, getDefaultState())
    }
}

const actions = {
    // use select all
    selectAll({
        commit
    }, orderInfo) {
        return new Promise((resolve, reject) => {
            const {
                obj,
                page,
                pageSize
            } = orderInfo
            selectAll({
                obj: obj,
                page: page,
                pageSize: pageSize
            }).then(response => {
                resolve(response.data)
            }).catch(error => {
                reject(error)
            })
        })
    },

    // use add menu
    add({
        commit
    }, orderInfo) {
        return new Promise((resolve, reject) => {
            const {
                orderId,
                patientId,
                doctorId
            } = orderInfo
            add({
                orderId: orderId,
                patientId: patientId,
                doctorId: doctorId
            }).then(response => {
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },

    allSave({
        commit
    }, orderInfo) {
        return new Promise((resolve, reject) => {
            const {
                patientId,
                allMedicine
            } = orderInfo
            allSave({
                patientId: patientId,
                allMedicine: allMedicine
            }).then(response => {
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },

    // use update
    update({
        commit
    }, orderInfo) {
        return new Promise((resolve, reject) => {
            const {
                orderId,
                patientId,
                doctorId
            } = orderInfo
            update({
                orderId: orderId,
                patientId: patientId,
                doctorId: doctorId
            }).then(response => {
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },

    // use delete 
    delete({
        commit
    }, ids) {
        return new Promise((resolve, reject) => {
            del(ids).then(response => {
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },

    // use delete 
    determine({
        commit
    }, orderId) {
        return new Promise((resolve, reject) => {
            determine({
                orderId: orderId
            }).then(response => {
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    }
}
export default {
    namespaced: true,
    state,
    mutations,
    actions
}
